package virtualstock.player;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import virtualstock.exception.InvalidParameterException;
import virtualstock.exception.PlayerNotFoundException;
import virtualstock.request.PlayerController;

/**
 * Servlet implementation class PlayerRemoveServlet
 */
public class PlayerRemoveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	private PlayerController playerController;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PlayerRemoveServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	PrintWriter out = response.getWriter();
    	String playIDStr = request.getParameter("playerID");
    	if (playIDStr == null) {
    		out.println("<meta http-equiv=\"refresh\" content=\"1;url=adminHomePage.jsp\">");
    		out.println("No player selected. <br> automated redirect in 1 second");
    		return;
    	}
    	Long playerID = new Long(playIDStr);
    	try{
    		playerController.removePlayer(playerID);
    		
    	} catch(InvalidParameterException e){
    		out.println(e.getLocalizedMessage());
    		e.printStackTrace();
    	} catch(PlayerNotFoundException e){
    		out.println(e.getLocalizedMessage());
    		e.printStackTrace();
    	} catch(Exception e){
    		e.printStackTrace();
    	}
    	System.out.println("++++++PlayerRemove Servlet : before redirection!");
    	response.sendRedirect("adminHomePage.jsp");
    	
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
